Skip to content

TYP: core.sorting #41285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 4, 2021
Merged

TYP: core.sorting #41285

merged 3 commits into from
May 4, 2021

Conversation

jbrockmendel
Copy link
Member

both mypy and i have a hard time with labels, lshape = map(list, zip(*map(maybe_lift, labels, lshape)))

@MarcoGorelli MarcoGorelli self-requested a review May 3, 2021 17:06
@MarcoGorelli
Copy link
Member

MarcoGorelli commented May 3, 2021

both mypy and i have a hard time with labels, lshape = map(list, zip(*map(maybe_lift, labels, lshape)))

I just tried

zip(*(maybe_lift(label, shape_) for label, shape_ in zip(labels, shape)))

and it seems to work:

diff --git a/pandas/core/sorting.py b/pandas/core/sorting.py
index ee62254052..dc4c21a3e4 100644
--- a/pandas/core/sorting.py
+++ b/pandas/core/sorting.py
@@ -147,9 +147,7 @@ def get_group_index(labels, shape: Shape, sort: bool, xnull: bool):
     if not xnull:
         # error: Incompatible types in assignment (expression has type
         # "List[_T]", variable has type "List[int]")
-        labels, lshape = map(  # type: ignore[assignment]
-            list, zip(*map(maybe_lift, labels, lshape))
-        )
+        labels, shape = zip(*(maybe_lift(label, shape_) for label, shape_ in zip(labels, shape)))
 
     labels = list(labels)
$ mypy pandas
Success: no issues found in 1283 source files

pandas/tests/test_sorting.py still passes too

@jbrockmendel jbrockmendel added the Typing type annotations, mypy/pyright type checking label May 3, 2021
@jbrockmendel
Copy link
Member Author

@MarcoGorelli is there a convenient way to get rid of the "*"? this feels like the kind of thing where the answer is hitting me in the face...

@MarcoGorelli
Copy link
Member

Could define unzip somewhere as suggested here https://stackoverflow.com/a/22115957 . Might make it less mysterious than zip(*

@jreback jreback added this to the 1.3 milestone May 4, 2021
@jreback jreback merged commit 601c43c into pandas-dev:master May 4, 2021
@jbrockmendel jbrockmendel deleted the typ-sorting branch May 4, 2021 14:00
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants